/**
 * 
 */
package com.oscarmovie.dao;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.hibernate.Hibernate;
import org.hibernate.Session;
import org.hibernate.transform.Transformers;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.oscarmovie.model.MovieRelationship;

/**
 * @author David
 *
 */
public class CacheDaoImpl extends HibernateDaoSupport implements CacheDao {

   private static List<MovieRelationship> movieCache = new ArrayList<MovieRelationship>();
   
   private int movieSize;
	
	public List<MovieRelationship> loadAllMovieRelationship(){		
		if (movieCache.size() == 0) {
			String sql = "select m.id movieId, "
			+"         mpac.award_id as awardId,               "
			+"         mpac.calendar_id as calendarId,            "
			+"         m_country.country_id as countryId,            "
			+"         mcg.category_id as categoryId,           "
			+"         mpac.type as Type   			"
			+"  from  "
			+"  movie m, "                        
			+"  mpac mpac, "                      
			+"  movie_country m_country, "               
			+"  movie_category mcg       "
			+"  where   "
			+"  m.id = mpac.movie_id " 
			+"  and m.id = m_country.movie_id "
			+"  and m.id= mcg.movie_id";
			
			long t1 = System.currentTimeMillis();
			movieCache = getSession().createSQLQuery(sql).
			addScalar("movieId", Hibernate.INTEGER).
			addScalar("awardId", Hibernate.INTEGER).
			addScalar("calendarId", Hibernate.INTEGER).
			addScalar("countryId", Hibernate.INTEGER).
			addScalar("categoryId", Hibernate.INTEGER).
			addScalar("Type", Hibernate.INTEGER).
			setResultTransformer(Transformers.aliasToBean(MovieRelationship.class)).  		
			setCacheable(true).list();
			long t2 = System.currentTimeMillis();
			System.out.println("CacheDaoImpl.loadAllMovieRelationship(): Time: "+(t2-t1)+", size: "+movieCache.size());
		}
		
		return movieCache;
	}		
	
	public Set<MovieRelationship> filterMovie(int calendarId, int categoryId, int countryId, int awardId, int personId, int type) {
		Set<MovieRelationship> filteredMovies = new HashSet<MovieRelationship>(); 		
		long t1 = System.currentTimeMillis();
		for (MovieRelationship mrs: movieCache) {
			if ((calendarId == 0 ? true: mrs.getCalendarId() == calendarId) &&					
				(categoryId == 0 ? true : mrs.getCategoryId() == categoryId) && 
				(countryId == 0 ? true : mrs.getCountryId() == countryId) && 				
				(awardId == 0 ? true : mrs.getAwardId() == awardId) && 
				(personId == 0 ? true :  mrs.getPersonId() == personId) && 
				(type == 0 ? true : mrs.getType() == type)
			) {
				filteredMovies.add(mrs);
			}
		}
		long t2 = System.currentTimeMillis();
		return filteredMovies;
	}
	
	public String getMovieIdString(int calendarId, int categoryId, int countryId, int awardId, int personId, int type) {
		StringBuilder sb = new StringBuilder();
		Set<MovieRelationship> filteredMovies = filterMovie(calendarId, categoryId, countryId,awardId, personId, type);		
		int i = 0;
		for (MovieRelationship mrs : filteredMovies) {
			i++;
			sb.append(mrs.getMovieId()).append(",");
			if (i == 998) {
				sb.append("0").append(") or id in (");
			}
		}
		sb.append("0");
		this.setMovieSize(i);
		
		return sb.toString();	
	}

	/**
	 * @return the movieSize
	 */
	public int getMovieSize() {
		return movieSize;
	}

	/**
	 * @param movieSize the movieSize to set
	 */
	public void setMovieSize(int movieSize) {
		this.movieSize = movieSize;
	}
	
}
